package langchain4j.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import langchain4j.entity.Doctor;
import langchain4j.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @GetMapping("/doctorList")
    public ResponseEntity<Map<String, Object>> getDoctorList(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "9") int size,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "department", required = false) String department
    ) {
        // 计算偏移量
        int offset = (page - 1) * size;

        // 调用Mapper层查询
        List<Doctor> doctorList = doctorService.selectDoctorListByPage(offset, size, name, title,department);
        int total = doctorService.countDoctorList(name, title,department);

        // 计算总页数
        int totalPages = (int) Math.ceil((double) total / size);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", doctorList);
        response.put("total", total);
        response.put("pages", totalPages);
        response.put("current", page);
        response.put("size", size);

        return ResponseEntity.ok(response);
    }
}
